"""
GUI综合练习-用户登录
"""
import tkinter as tk
from tkinter import messagebox

class LoginWindow:
    def __init__(self, root):
        # 创建登录窗口界面

        root.title("用户登录")
        root.geometry("300x200")
        # 使用Frame容器化管理组件
        iframe = tk.Frame(root, width=300, height=200, relief='ridge')
        iframe.grid(padx=20, pady=30)
        # 用户名输入
        name_label = tk.Label(iframe, text="用户名：")
        self.name_entry = tk.Entry(iframe)
        name_label.grid(row=0, column=0, sticky="e")
        self.name_entry.grid(row=0, column=1, sticky="w")
        # 密码的输入
        pwd_label = tk.Label(iframe, text="密码：", padx=10)
        self.pwd_entry = tk.Entry(iframe, show="*")
        pwd_label.grid(row=1, column=0, sticky="e")
        self.pwd_entry.grid(row=1, column=1, sticky="w")
        # 登录按钮
        login_button = tk.Button(iframe, text="登录", width=10, command=self.login)
        login_button.grid(row=2, column=0, columnspan=2)
        root.mainloop()

    def login(self):
        print("--------------")
        # 获取用户名和密码
        username = self.name_entry.get()
        password = self.pwd_entry.get()
        # 验证用户名和密码
        if username == "admin" and password == "123":
            # 登录成功，弹出提示框
            tk.messagebox.showinfo("登录成功", "欢迎登录")
            # 销毁当前窗口，打开主界面
            root.destroy()
            # 创建主界面窗口
            main_window = tk.Tk()
            MainWindow(main_window)
        else:
            # 登录失败，弹出提示框
            tk.messagebox.showerror("登录失败", "用户名或密码错误")

class MainWindow:
    def __init__(self, main):
        # 创建主窗口界面
        main.title("主窗口")
        main.geometry("400x300")
        label = tk.Label(main, text="欢迎使用主窗口")
        label.pack(pady=20)


# __main__
if __name__ == "__main__":
    root = tk.Tk()
    login_window = LoginWindow(root)




